#include "asynch_write_queue.h"
#include "srv_asynch_man.h"
#include "Task_Base/CriticalSection.h"
#include "TcpPackBuf/TcpPackBuf.h"
#include <string.h>

enum ENUM_WRITE_OPERATION
{
    ENUM_WRITE_OPERATION_POST_DATA,				//套接字post数据
    ENUM_WRITE_OPERATION_DEL_SOCKET,				//关闭套接字关联buf
    ENUM_WRITE_OPERATION_NORMAL,					//正常buf数据
};

typedef struct
{
    int                 	code ;			//操作码
    int						socket;			//附加参数
}Write_Queue_Addition_Struct;

Asynch_Write_Queue::Asynch_Write_Queue(Srv_Asynch_Man *pAsynch_man):m_pAsynch_man(pAsynch_man)
{
    m_poCriSection_write_buf  = new CCriticalSection;
}

Asynch_Write_Queue::~Asynch_Write_Queue()
{
    close();
    delete m_poCriSection_write_buf;
}

void Asynch_Write_Queue::awq_add_service_handler(int client_socket)
{
    CCriticalAutoLock loAutoLock(*m_poCriSection_write_buf);

    map_socket_write_buf[client_socket] =new std::string ;
    //map_socket_write_num[client_socket] =new int(0);
    //map_socket_write_num2[client_socket] =new int(0);
    map_socket_write_num3[client_socket] =new int(-1);
}

void Asynch_Write_Queue::awq_append_write_buf(int client_socket, const char *pBuf, int nlen)
{
    if( nlen == 0 )
        return ;

    Write_Queue_Addition_Struct write_queue_addition_struct ;
    write_queue_addition_struct.code = ENUM_WRITE_OPERATION_NORMAL;
    write_queue_addition_struct.socket = client_socket;

    std::string str_buf;
    str_buf.append((char*)&write_queue_addition_struct,sizeof(Write_Queue_Addition_Struct));  //加入buf前几个字节 附加结构
    str_buf.append(pBuf,nlen);				//数据接着加入
    putq(str_buf);		//入队
}

void Asynch_Write_Queue::awq_post_send_socket(int client_socket)
{
    Write_Queue_Addition_Struct write_queue_addition_struct ;
    write_queue_addition_struct.code = ENUM_WRITE_OPERATION_POST_DATA;
    write_queue_addition_struct.socket = client_socket;

    std::string str_buf;
    str_buf.append((char*)&write_queue_addition_struct,sizeof(Write_Queue_Addition_Struct));  //加入buf前几个字节 附加结构
    putq(str_buf);		//入队
}

void Asynch_Write_Queue::awq_post_delete_socket( int client_socket )
{
    Write_Queue_Addition_Struct write_queue_addition_struct ;
    write_queue_addition_struct.code = ENUM_WRITE_OPERATION_DEL_SOCKET;
    write_queue_addition_struct.socket = client_socket;

    std::string str_buf;
    str_buf.append((char*)&write_queue_addition_struct,sizeof(Write_Queue_Addition_Struct));  //加入buf前几个字节 附加结构
    putq(str_buf);		//入队
}
#include <iostream>
using namespace std;
int Asynch_Write_Queue::svc()
{
    char * szPackBuff = new char[4096];
    CTcpPackBuf   m_oTcpPackBuff;  //网络层打包

    std::string str ="";

    while(getq(str))	//出队
    {
        if (str=="")	//为空为退出标志 0字节 情况
        {
            return true;
        }

        const char *pSocket		= str.data();
        int			buf_len		= str.size();

        Write_Queue_Addition_Struct *addition_struct = (Write_Queue_Addition_Struct *)pSocket ;

        switch(addition_struct->code)
        {

        case ENUM_WRITE_OPERATION_POST_DATA:		//发送数据
            {
                string * socket_write_buf = find_socket_write_buf(addition_struct->socket);//查找套接字对于的缓冲

                if(socket_write_buf && (socket_write_buf->size() > 0) )
                {
                    if(m_pAsynch_man)
                    {
                       //int nub =  socket_write_buf->size();

                        bool bl    = m_pAsynch_man->handle_send_passive(addition_struct->socket,*socket_write_buf);//投递发送数据
                        if(bl)
                        {
                            *(map_socket_write_num3[addition_struct->socket]) = 0;
                        }else
                        {
                            *(map_socket_write_num3[addition_struct->socket]) = 2;
                        }

                       //*(map_socket_write_num[addition_struct->socket]) += (nub - socket_write_buf->size());
                    }
                }
            }
            break;
        case ENUM_WRITE_OPERATION_DEL_SOCKET:		//删除套接字关联的buf数据
            {
                //int A1 = *(map_socket_write_num[addition_struct->socket]);
                //int A2 = *(map_socket_write_num2[addition_struct->socket]);
                int A3  = *(map_socket_write_num3[addition_struct->socket]);
                //cout << "ENUM_WRITE_OPERATION_DEL_SOCKET :" << A1  << " : " <<  A2  <<" : " <<  A3 << endl;
                delete_service_handler(addition_struct->socket);
                int num =   map_socket_write_buf.size();
                 cout << "delete_service_handler :" << A3 << " : " <<  num << endl;
            }
            break;
        case ENUM_WRITE_OPERATION_NORMAL:
            {

                const char  *pBuf       = pSocket + sizeof(Write_Queue_Addition_Struct);//分离出数据
                int         nBufLen     = buf_len - sizeof(Write_Queue_Addition_Struct);//后续数据长度

                memset(szPackBuff,0,4096);

                int  lwPackBuffLen = m_oTcpPackBuff.Pack(pBuf,nBufLen,szPackBuff,4096);
                if (lwPackBuffLen)
                {
                    string * socket_write_buf = find_socket_write_buf(addition_struct->socket);//查找套接字对于的缓冲

                    if( socket_write_buf->size() > 0 )
                    {
                        if(socket_write_buf)
                        {
                             //int nub =  socket_write_buf->size();
                            socket_write_buf->append(szPackBuff,lwPackBuffLen);//添加到套接字发送buf
                             //*(map_socket_write_num2[addition_struct->socket]) += (socket_write_buf->size() - nub);
                        }

                    }else
                    {
                        if(socket_write_buf)
                        {
                             //int nub =  socket_write_buf->size();
                            socket_write_buf->append(szPackBuff,lwPackBuffLen);//添加到套接字发送buf
                            //*(map_socket_write_num2[addition_struct->socket]) += (socket_write_buf->size() - nub);
                        }

                        if(m_pAsynch_man)
                        {
                            //int nub =  socket_write_buf->size();
                             bool bl   = m_pAsynch_man->handle_send_active(addition_struct->socket,*socket_write_buf);//投递发送数据
                             if(bl)
                                 *(map_socket_write_num3[addition_struct->socket]) = 1;

                            //*(map_socket_write_num[addition_struct->socket]) += (nub - socket_write_buf->size());
                        }

                    }
                }
            }
            break;
        }
    }
        delete []szPackBuff;

    return 1;
}
void Asynch_Write_Queue::delete_service_handler(int client_socket)
{
    CCriticalAutoLock loAutoLock(*m_poCriSection_write_buf);

    std::map<int,std::string*>::iterator it_handle = map_socket_write_buf.find(client_socket);

    if (it_handle != map_socket_write_buf.end())
    {
        delete it_handle->second;

        map_socket_write_buf.erase(it_handle);
    }

    std::map<int,int *>::iterator it_handle_1 = map_socket_write_num3.find(client_socket);

    if (it_handle_1 != map_socket_write_num3.end())
    {
        delete it_handle_1->second;

        map_socket_write_num3.erase(it_handle_1);
    }

}

string *Asynch_Write_Queue::find_socket_write_buf(int client_socket)
{
    CCriticalAutoLock loAutoLock(*m_poCriSection_write_buf);

    std::map<int,std::string*>::iterator it =map_socket_write_buf.find(client_socket);
    if (it != map_socket_write_buf.end())
    {
        return it->second;
    }
    return	NULL;
}

int Asynch_Write_Queue::close()
{
    std::string str("");
    int nthread  =thr_count();

    for (int i =0 ;i<nthread ;i++)
        putq(str);                     //0字节的

    return 0;
}
